var log = console.log.bind(console)
// var e = self => document.getElementById(self)

// var imgFromePath = function (path) {
//     var img = new Image()
//     img.src = path
//     img.onload = function () {
//         return img
//     }
//     return img
// }
// 判断是否相撞
// ball.left < o.right && ball.right > o.left && 。。。。
var checkPound = function (o, b) {
    if (b.x + b.image.width >= o.x && b.x <= o.x + o.image.width) {
        if (b.y + b.image.height >= o.y && b.y <= o.y + o.image.height) {
            log('相撞')
            return true
        }
    }
    return false
}

// 判断碰撞方向
var poundDirrection = function(o, b) {
    let X = false, Y = false
    if (b.x < o.x && b.x + b.image.width > o.x || b.x < o.x + o.image.width && b.x + b.image.width > o.x + o.image.width) {
        X = true
    }
    if (b.y < o.y && b.y + b.image.height > o.y || b.y < o.y + o.image.height && b.y + b.image.height > o.y + o.image.height) {
        Y = true
    }
    return  { 'X': X, 'Y': Y }
}



// 实现自定义事件的订阅

var EventTarget = function() {
    this.eventHandlers = {}
}

EventTarget.prototype = {
    addHandler: function(type, handler) {
        if (typeof this.eventHandlers[type] == "undefined") {
            this.eventHandlers[type] = []
        }
        this.eventHandlers[type].push(handler)
    },
    removeHandler: function(type, handler) {
        if (this.eventHandlers[event.type] instanceof Array) {
            var handlers = this.eventHandlers[event.type]
            var index 
            handlers.some(function (h, i) {
                h === handler
                index = i
            })
            handlers.slice[i, 1]
        }

    },
    fire: function(event) {
        if (!event.target) {
            event.target = this
        }
        if (this.eventHandlers[event.type] instanceof Array) {
            let handlers = this.eventHandlers[event.type]
            for (let i = 0; i < handlers.length; i++) {
                handlers[i](event)
            }
            console.log( handlers instanceof Array)
        }
    }
}

// foo()
// function foo(){
//     console.log(1)
// }
// var foo = function(){
//     console.log(2)
// }
// function foo(){
//     console.log(3)
// }

// getName()
// function getName() {
//     console.log(2)
// }
// var getName = function() {
//     console.log(4)
// }